package com.scwuhua.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.MeteorologicalSoilConstants;
import com.ruoyi.common.enumCode.ErrorCode;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.scwuhua.domain.Account;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.scwuhua.mapper.DeviceMapper;
import com.scwuhua.domain.Device;
import com.scwuhua.service.IDeviceService;
import org.springframework.web.client.RestTemplate;

/**
 * 设备信息Service业务层处理
 * 
 * @author wuhua
 * @date 2023-05-11
 */
@Slf4j
@Service
public class DeviceServiceImpl implements IDeviceService 
{
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private MeteorologicalSoilServiceImpl meteorologicalSoilService;
    @Value("${MeteorologicalSoil.authentication.username}")
    private String username;
    @Value("${MeteorologicalSoil.pagingData.equipmentUrl}")
    private String equipmentUrl;

    /**
     * 查询设备信息列表
     *
     * @return 设备信息
     */
    @Override
    public List<Device> selectDeviceList()
    {
        /*所需参数
        *  token  username
        *  请求方式 get
        *  请求地址 http://47.105.215.208:8005/user/282025131-1
        *  响应体 用户信息 设备信息{ 详细 }
        *  判断是否存在token
        * */
        String token = meteorologicalSoilService.gainToken();
        String url = equipmentUrl+username;
        String body = meteorologicalSoilService.sendGetRequest(token, url);
        Account account = JSONObject.parseObject(body, Account.class);
        log.info(body);
        //deviceMapper.selectDeviceList(device); 查数据库
        return account.getDevices();
    }

    /**
     * 一键同步功能
     * */
    @Override
    public int synchroData() {
        List<Device> devices = selectDeviceList();
        if (StringUtils.isEmpty(devices)){
            throw new ServiceException(ErrorCode.DATA_ACQUISITION_EXCEPTION.getMessage());
        }
        for (Device device : devices) {
            if ( deviceMapper.selectDeviceById(device.getId()) == null){
                deviceMapper.insertDevice(device);
            }
        }
        return 0;
    }


    /**
     * 查询设备信息
     *
     * @param id 设备信息主键
     * @return 设备信息
     */
    @Override
    public Device selectDeviceById(Integer id)
    {
        return deviceMapper.selectDeviceById(id);
    }

    /**
     * 新增设备信息
     * 
     * @param device 设备信息
     * @return 结果
     */
    @Override
    public int insertDevice(Device device)
    {
        device.setCreateTime(DateUtils.getNowDate());
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改设备信息
     * 
     * @param device 设备信息
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除设备信息
     * 
     * @param ids 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(Integer[] ids)
    {
        return deviceMapper.deleteDeviceByIds(ids);
    }

    /**
     * 删除设备信息信息
     * 
     * @param id 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceById(Integer id)
    {
        return deviceMapper.deleteDeviceById(id);
    }


}
